package database;

import java.sql.*;

public class MySQLUtils {

    private static Connection connection = null;
    private static Statement statement = null;
    private static ResultSet resultSet = null;

    private static Connection getConnection() throws Exception {
        String url = "jdbc:mysql://49.232.9.251:3306/web";
        String user = "root";
        String pwd = "password";
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取连接
        connection = DriverManager.getConnection(url, user, pwd);
        return connection;
    }

    //插入数据
    public static void insert(String mobile, String user, String passwd) throws Exception {
        PreparedStatement preparedStatement = getConnection().prepareStatement("insert into user values(?,?,?)");
        preparedStatement.setString(1, mobile);
        preparedStatement.setString(2, user);
        preparedStatement.setString(3, passwd);
        preparedStatement.executeUpdate();
    }

    //查询数据
    public static ResultSet getResultSet(String sql) throws Exception {
        resultSet = getStatement().executeQuery(sql);
        return resultSet;
    }

    //获取语句
    private static Statement getStatement() throws Exception {
        statement = getConnection().createStatement();
        return statement;
    }
}
